package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import m.jclecai.com.R;

import java.text.DecimalFormat;
import java.util.List;

import m.jclecai.com.bean.ZqBQCItem;
import m.jclecai.com.dialog.BQCDialog;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.Utils;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/4/10.
 */

public class ExpandableBQCAdapter extends BaseExpandableListAdapter {
    private Context context;

    private List<String> groups;
    List<List<ZqBQCItem>> lists;
    private NotifyTextViewSetChanged changed;
    private BQCDialog bqcDialog;
    DecimalFormat df = new DecimalFormat("#.00");

    public ExpandableBQCAdapter() {
    }
    public ExpandableBQCAdapter(Context context, List<String> groups,
                                List<List<ZqBQCItem>> lists, NotifyTextViewSetChanged changed) {
        this.context = context;
        this.groups = groups;
        this.lists = lists;
        this.changed = changed;
    }
    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return lists.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return lists.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_parent, null);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.list_item_parent_title.setText(groups.get(groupPosition));
        if (isExpanded) {
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.cz);
        } else {
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.d0);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChlidViewHolder chlidViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_zq_bf, null);
            chlidViewHolder = new ChlidViewHolder(convertView);
            convertView.setTag(chlidViewHolder);
        } else {
            chlidViewHolder = (ChlidViewHolder) convertView.getTag();
        }
        final ZqBQCItem item = (ZqBQCItem) getChild(groupPosition, childPosition);
        final String[] codes = item.getCodes();

        chlidViewHolder.away_team_name.setText(Html.fromHtml(codes[4]));
        chlidViewHolder.home_team_name.setText(Html.fromHtml(codes[3]));
        chlidViewHolder.item_name.setText(codes[2]);//世界杯预选赛
        chlidViewHolder.item_time.setText(codes[7].substring(5, 16) + "\n" + codes[1]);

        final List<String[]> rqsflists = FastOk.getList(codes[16], String[].class);


        chlidViewHolder.jczq_select_bf.setText("点击选择半全场");
//        chlidViewHolder.jczq_select_bf.setTextColor(Color.parseColor("#D2D2D2"));

        chlidViewHolder.jczq_select_bf.setTag(item);

        chlidViewHolder.jczq_select_bf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bqcDialog = new BQCDialog(context, R.style.delete_dialog, new BQCDialog.BQCDialogListener() {
                    @Override
                    public void onClick(View view) {
                        switch (view.getId()) {
                            case R.id.dialog_confirm:
                                if (item.getCodeList().size() != 0) {
                                    StringBuffer buffer = new StringBuffer();
                                    for (int i = 0; i < item.getCodeList().size(); i++) {
                                        buffer.append(item.getCodeList().get(i));
                                        buffer.append(",");
                                    }
                                    buffer.deleteCharAt(buffer.length() - 1);
                                    chlidViewHolder.jczq_select_bf.setText(buffer.toString());
                                    chlidViewHolder.jczq_select_bf.setTextColor(Color.WHITE);
                                    isAdd(item);
                                } else {
                                    chlidViewHolder.jczq_select_bf.setText("点击选择半全场");
                                    chlidViewHolder.jczq_select_bf.setTextColor(Color.WHITE);
                                    isAdd(item);
                                }
                                bqcDialog.cancel();
                                break;
                            case R.id.dialog_cancel:
                                bqcDialog.cancel();
                                break;
                            case R.id.jczq_bf_1:
                                TextView textView1 = (TextView) view
                                        .findViewById(R.id.jczq_bf_1);
                                if(!item.getCodeList().contains("胜胜")){
                                    item.getCodeList().add("胜胜");
                                    item.getList().add("3-3");
                                    item.getOdd_sp().add(rqsflists.get(4)[0]);
                                    textView1.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView1.setTextColor(Color.WHITE);
                                }else {
                                    item.getList().remove("3-3");
                                    item.getCodeList().remove("胜胜");
                                    item.getOdd_sp().remove(rqsflists.get(4)[0]);
                                    textView1.setTextColor(Color.BLACK);
                                    textView1.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_2:
                                TextView textView2 = (TextView) view
                                        .findViewById(R.id.jczq_bf_2);
                                if(!item.getCodeList().contains("胜平")){
                                    item.getCodeList().add("胜平");
                                    item.getList().add("3-1");
                                    item.getOdd_sp().add(rqsflists.get(4)[1]);
                                    textView2.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView2.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("胜平");
                                    item.getList().remove("3-1");
                                    item.getOdd_sp().remove(rqsflists.get(4)[1]);
                                    textView2.setTextColor(Color.BLACK);
                                    textView2.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_3:
                                TextView textView3 = (TextView) view
                                        .findViewById(R.id.jczq_bf_3);
                                if(!item.getCodeList().contains("胜负")){
                                    item.getCodeList().add("胜负");
                                    item.getList().add("3-0");
                                    item.getOdd_sp().add(rqsflists.get(4)[2]);
                                    textView3.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView3.setTextColor(Color.WHITE);
                                }else {
                                    item.getList().remove("3-0");
                                    item.getCodeList().remove("胜负");
                                    item.getOdd_sp().remove(rqsflists.get(4)[2]);
                                    textView3.setTextColor(Color.BLACK);
                                    textView3.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_4:
                                TextView textView4 = (TextView) view
                                        .findViewById(R.id.jczq_bf_4);
                                if(!item.getCodeList().contains("平胜")){
                                    item.getCodeList().add("平胜");
                                    item.getList().add("1-3");
                                    item.getOdd_sp().add(rqsflists.get(4)[3]);
                                    textView4.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView4.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("平胜");
                                    item.getList().remove("1-3");
                                    item.getOdd_sp().remove(rqsflists.get(4)[3]);
                                    textView4.setTextColor(Color.BLACK);
                                    textView4.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_5:
                                TextView textView5 = (TextView) view
                                        .findViewById(R.id.jczq_bf_5);
                                if(!item.getCodeList().contains("平平")){
                                    item.getCodeList().add("平平");
                                    item.getList().add("1-1");
                                    item.getOdd_sp().add(rqsflists.get(4)[4]);
                                    textView5.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView5.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("平平");
                                    item.getList().remove("1-1");
                                    item.getOdd_sp().remove(rqsflists.get(4)[4]);
                                    textView5.setTextColor(Color.BLACK);
                                    textView5.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_6:
                                TextView textView6 = (TextView) view
                                        .findViewById(R.id.jczq_bf_6);
                                if(!item.getCodeList().contains("平负")){
                                    item.getCodeList().add("平负");
                                    item.getList().add("1-0");
                                    item.getOdd_sp().add(rqsflists.get(4)[5]);
                                    textView6.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView6.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("平负");
                                    item.getList().remove("1-0");
                                    textView6.setTextColor(Color.BLACK);
                                    item.getOdd_sp().remove(rqsflists.get(4)[5]);
                                    textView6.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_7:
                                TextView textView7 = (TextView) view
                                        .findViewById(R.id.jczq_bf_7);
                                if(!item.getCodeList().contains("负胜")){
                                    item.getCodeList().add("负胜");
                                    item.getList().add("0-3");
                                    item.getOdd_sp().add(rqsflists.get(4)[6]);
                                    textView7.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView7.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("负胜");
                                    item.getList().remove("0-3");
                                    item.getOdd_sp().remove(rqsflists.get(4)[6]);
                                    textView7.setTextColor(Color.BLACK);
                                    textView7.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_8:
                                TextView textView8 = (TextView) view
                                        .findViewById(R.id.jczq_bf_8);
                                if(!item.getCodeList().contains("负平")){
                                    item.getCodeList().add("负平");
                                    item.getList().add("0-1");
                                    item.getOdd_sp().add(rqsflists.get(4)[7]);
                                    textView8.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView8.setTextColor(Color.WHITE);
                                }else {
                                    item.getList().remove("0-1");
                                    item.getCodeList().remove("负平");
                                    item.getOdd_sp().remove(rqsflists.get(4)[7]);
                                    textView8.setTextColor(Color.BLACK);
                                    textView8.setBackgroundColor(Color.WHITE);
                                }
                                break;
                            case R.id.jczq_bf_9:
                                TextView textView9 = (TextView) view
                                        .findViewById(R.id.jczq_bf_9);
                                if(!item.getCodeList().contains("负负")){
                                    item.getCodeList().add("负负");
                                    item.getList().add("0-0");
                                    item.getOdd_sp().add(rqsflists.get(4)[8]);
                                    textView9.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
                                    textView9.setTextColor(Color.WHITE);
                                }else {
                                    item.getCodeList().remove("负负");
                                    item.getList().remove("0-0");
                                    item.getOdd_sp().remove(rqsflists.get(4)[8]);
                                    textView9.setTextColor(Color.BLACK);
                                    textView9.setBackgroundColor(Color.WHITE);
                                }
                                break;
                        }
                    }
                });
                bqcDialog.setCancelable(false);
                bqcDialog.setCanceledOnTouchOutside(false);
                // 标题
                TextView title = (TextView) bqcDialog.getCustomView().findViewById(R.id.dialog_title);
                title.setText(codes[3]+ "　　VS　　"+codes[4]);
                // 胜
                TextView titles1 = (TextView) bqcDialog.getCustomView().findViewById(R.id.jczq_bf_1);
                titles1.setText("胜胜\n" + df.format(Double.parseDouble(rqsflists.get(4)[0])));
                if (item.getCodeList().contains("胜胜")) {
                    titles1.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles1.setTextColor(Color.WHITE);
                } else {
                    titles1.setTextColor(Color.BLACK);
                    titles1.setBackgroundColor(Color.WHITE);
                }

                TextView titles2 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_2);
                titles2.setText("胜平\n" + df.format(Double.parseDouble(rqsflists.get(4)[1])));
                if (item.getCodeList().contains("胜平")) {
                    titles2.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles2.setTextColor(Color.WHITE);
                } else {
                    titles2.setTextColor(Color.BLACK);
                    titles2.setBackgroundColor(Color.WHITE);
                }

                TextView titles3 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_3);
                titles3.setText("胜负\n" + df.format(Double.parseDouble(rqsflists.get(4)[2])));
                if (item.getCodeList().contains("胜负")) {
                    titles3.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles3.setTextColor(Color.WHITE);
                } else {
                    titles3.setTextColor(Color.BLACK);
                    titles3.setBackgroundColor(Color.WHITE);
                }

                TextView titles4 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_4);
                titles4.setText("平胜\n" + df.format(Double.parseDouble(rqsflists.get(4)[3])));
                if (item.getCodeList().contains("平胜")) {
                    titles4.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles4.setTextColor(Color.WHITE);
                } else {
                    titles4.setTextColor(Color.BLACK);
                    titles4.setBackgroundColor(Color.WHITE);
                }

                TextView titles5 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_5);
                titles5.setText("平平\n" + df.format(Double.parseDouble(rqsflists.get(4)[4])));
                if (item.getCodeList().contains("平平")) {
                    titles5.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles5.setTextColor(Color.WHITE);
                } else {
                    titles5.setTextColor(Color.BLACK);
                    titles5.setBackgroundColor(Color.WHITE);
                }

                TextView titles6 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_6);
                titles6.setText("平负\n" + df.format(Double.parseDouble(rqsflists.get(4)[5])));
                if (item.getCodeList().contains("平负")) {
                    titles6.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles6.setTextColor(Color.WHITE);
                } else {
                    titles6.setTextColor(Color.BLACK);
                    titles6.setBackgroundColor(Color.WHITE);
                }
                TextView titles7 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_7);
                titles7.setText("负胜\n" + df.format(Double.parseDouble(rqsflists.get(4)[6])));
                if (item.getCodeList().contains("负胜")) {
                    titles7.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles7.setTextColor(Color.WHITE);
                } else {
                    titles7.setTextColor(Color.BLACK);
                    titles7.setBackgroundColor(Color.WHITE);
                }
                TextView titles8 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_8);
                titles8.setText("负平\n" + df.format(Double.parseDouble(rqsflists.get(4)[7])));
                if (item.getCodeList().contains("负平")) {
                    titles8.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles8.setTextColor(Color.WHITE);
                } else {
                    titles8.setTextColor(Color.BLACK);
                    titles8.setBackgroundColor(Color.WHITE);
                }

                TextView titles9 = (TextView) bqcDialog.getCustomView()
                        .findViewById(R.id.jczq_bf_9);
                titles9.setText("负负\n" + df.format(Double.parseDouble(rqsflists.get(4)[8])));
                if (item.getCodeList().contains("负负")) {
                    titles9.setBackgroundColor(android.graphics.Color
                            .parseColor("#FF0026"));
                    titles9.setTextColor(Color.WHITE);
                } else {
                    titles9.setTextColor(Color.BLACK);
                    titles9.setBackgroundColor(Color.WHITE);
                }
                bqcDialog.show();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder {
        private final TextView list_item_parent_title;
        private final ImageView list_item_parent_icon;

        public GroupViewHolder(View view) {
            list_item_parent_title = (TextView) view.findViewById(R.id.list_item_parent_title);
            list_item_parent_icon = (ImageView) view.findViewById(R.id.list_item_parent_icon);
        }
    }

    static class ChlidViewHolder {
        private final TextView item_name;
        private final TextView item_time;
        private final TextView home_team_name;
        private final TextView away_team_name;
        private final TextView jczq_select_bf;

        public ChlidViewHolder(View view) {
            item_name = (TextView) view.findViewById(R.id.item_name);
            item_time = (TextView) view.findViewById(R.id.item_time);
            home_team_name = (TextView) view.findViewById(R.id.home_team_name);
            away_team_name = (TextView) view.findViewById(R.id.away_team_name);
            jczq_select_bf = (TextView) view.findViewById(R.id.jczq_select_bf);
        }
    }

    private void isAdd(ZqBQCItem item) {
        if (item.getCodeList().size() != 0) {
            if (!Utils.ZQ_BQC_ITEM_LIST.contains(item)) {
                Utils.ZQ_BQC_ITEM_LIST.add(item);
            }
        } else {
            if (Utils.ZQ_BQC_ITEM_LIST.contains(item)) {
                Utils.ZQ_BQC_ITEM_LIST.remove(item);
            }
        }
        changed.onClickSetChangedText();
    }
}
